﻿/*
 * Types.h
 *
 */

#ifndef __GDK_TYPES_H__
#define __GDK_TYPES_H__


#ifdef _WIN32

#define _MFC_OVERRIDES_NEW


#if _MSC_VER > 1200

#ifndef WINVER
#define WINVER  0x0501      /* version 5.0 */
#endif /* !WINVER */

#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0501
#endif

#ifndef _WIN32_IE
#define _WIN32_IE 0x0600
#endif

#ifndef _WIN32_WINDOWS
#define _WIN32_WINDOWS 0x0410
#endif

#ifndef _WIN32_IE
#define _WIN32_IE 0x0600
#endif

#else

#ifndef WINVER
#define WINVER  0x0400      /* version 4.0 */
#endif /* !WINVER */

#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0400
#endif

#ifndef _WIN32_IE
#define _WIN32_IE 0x0400
#endif

#ifndef _WIN32_WINDOWS
#define _WIN32_WINDOWS 0x0400
#endif

#ifndef _WIN32_IE
#define _WIN32_IE 0x0400
#endif

#endif

#if defined(_WIN32)
#	pragma warning(push)
#	pragma warning(disable:4786 4595)
#endif

#endif


#include <sys/types.h>
#include <stddef.h>
#include <assert.h>


#ifdef _WIN32
#	ifndef _MT
#		error Runtime must be Multi-threaded
#	endif
#	if !defined(_UNICODE)
#		error Must be use Unicode Character Set
#	endif
#endif


#if defined(DEBUG)
#   define _DEBUG   1
#endif


#define GDK_VERSION_INT(a, b, c)	(a << 24 | b << 16 | c << 8)
#define GDK_VERSION_DOT(a, b, c)	__TEXT__(#a) __TEXT__(".") __TEXT__(#b) __TEXT__(".") __TEXT__(#c)
#define GDK_VERSION(a, b, c)		GDK_VERSION_DOT(a, b, c)

#define GDK_CORE_VERSION_MAJOR	2
#define GDK_CORE_VERSION_MINOR	0
#define GDK_CORE_VERSION_MICRO	0
#define GDK_CORE_VERSION_INT	GDK_VERSION_INT(GDK_CORE_VERSION_MAJOR,\
												GDK_CORE_VERSION_MINOR,\
												GDK_CORE_VERSION_MICRO)
#define GDK_CORE_VERSION		GDK_VERSION(GDK_CORE_VERSION_MAJOR,\
											GDK_CORE_VERSION_MINOR,\
											GDK_CORE_VERSION_MICRO)


#if !defined(_MSC_VER)
#	define GDK_CORE_DLL_BUILD
#endif

/*
 * __LITTLE_ENDIAN__ 低序字节存储在起始地址
 * __BIG_ENDIAN__    高序字节存储在起始地址
 */
#if !defined(__BIG_ENDIAN__) && !defined(__LITTLE_ENDIAN__)
#	define __LITTLE_ENDIAN__	//默认低序字节存储在起始地址
#elif defined(__BIG_ENDIAN__) && defined(__LITTLE_ENDIAN__)
#	error "__LITTLE_ENDIAN__, __BIG_ENDIAN__ 不能同时被定义"
#endif


#if defined(_UNICODE)
#	if !defined(___TEXT___)
#		define  ___TEXT___(x)	L##x
#	endif
#else
#	if !defined(___TEXT___)
#		define  ___TEXT___(x)	x
#	endif
#endif
#define __TEXT__(x)	___TEXT___(x)


#define __GDK_FILE__		__TEXT__(__FILE__)
#define __GDK_FUNCTION__	__TEXT__(__FUNCTION__)


#if defined(_MSC_VER)		// for MSVC

#	define __INLINE__		__forceinline
#	define __CDECL__		__cdecl
#	define __FASTCALL__		__fastcall
#	define __DECLSPEC__(x)	__declspec(x)
#	define __DLLEXPORT__	dllexport
#	define __DLLIMPORT__	dllimport

#else	// Linux, IOS, Android

#	define __INLINE__		__inline__ __attribute__((always_inline))
#	define __CDECL__		//__attribute__((__cdecl__))
#	define __FASTCALL__		__attribute__((__fastcall__))
#	define __DECLSPEC__(x)	__attribute__((x))
#	define __DLLEXPORT__	visibility("default")
#	define __DLLIMPORT__	visibility("default")

#endif


#if defined(_MSC_VER) // for MSVC
#	define __PRINTF_FORMAT__(string, first)
#	define __SCANF_FORMAT__(string, first)
#	define __WPRINTF_FORMAT__(string, first)
#	define __WSCANF_FORMAT__(string, first)
#elif defined(__GNUC__) // for gcc
#	define __PRINTF_FORMAT__(string, first)   __attribute__((format(printf, string, first)))
#	define __SCANF_FORMAT__(string, first)    __attribute__((format(scanf, string, first)))
#	define __WPRINTF_FORMAT__(string, first)
#	define __WSCANF_FORMAT__(string, first)
#else
#	error
#endif
#if defined(_UNICODE)
#	define __PRINTF_FORMAT(string, first)   __WPRINTF_FORMAT__(string, first)
#	define __SCANF_FORMAT(string, first)    __WSCANF_FORMAT__(string, first)
#else
#	define __PRINTF_FORMAT(string, first)   __PRINTF_FORMAT__(string, first)
#	define __SCANF_FORMAT(string, first)    __SCANF_FORMAT__(string, first)
#endif

#if defined(_UNICODE)
#	define STRING_FMT	"%ls"
#else
#	define STRING_FMT	"%s"
#endif


#ifdef _WIN32
#	define GDK_ASSERT(exp)		assert(exp)
#else
#	ifdef _DEBUG
#		define GDK_ASSERT(exp)	assert(exp)
#	else
#		define GDK_ASSERT(exp)	((void)0)
#	endif
#endif


#define DECLARE_FUTILITY_VARIABLE1(type, name, value)	type __##name##__(value);
#define DECLARE_FUTILITY_VARIABLE2(type, name, value)	DECLARE_FUTILITY_VARIABLE1(type, name, value)
#define DECLARE_FUTILITY_VARIABLE(type, value)			DECLARE_FUTILITY_VARIABLE2(type, __LINE__, value)


typedef char                    utf8_t;
typedef unsigned char           ansi_t;

typedef unsigned char           uchar_t;
typedef unsigned long           ulong_t;

typedef long long               llong_t;
typedef unsigned long long      ullong_t;

typedef signed char             int8_t;
typedef short                   int16_t;
typedef int                     int32_t;

typedef unsigned char           uint8_t;
typedef unsigned short          uint16_t;
typedef unsigned int            uint32_t;

#if defined(_WIN32) || defined(__IOS__) || !defined(__x86_64__)
typedef long long               int64_t;
typedef unsigned long long      uint64_t;
#else
typedef long                    int64_t;
typedef unsigned long           uint64_t;
#endif

#if defined(_WIN32)
#if defined(_M_X64)
typedef int64_t                 ssize_t;
#else
typedef long                    ssize_t;
#endif
#endif

#if defined(_WIN32)
typedef int32_t                 pid_t;
#	define gettid               GetCurrentThreadId
#elif defined(__IOS__)
#	define gettid()             (uintptr_t)(pthread_self())
#elif !defined(__ANDROID__)
#	include <sys/syscall.h>
#	if !defined(gettid)
#		define gettid()         syscall(__NR_gettid)
#	endif
#endif


#if defined(__IOS__) || (defined(_MSC_VER) && (_MSC_VER >= 1600))
#	include <stdint.h>
#else

#if !defined(INT8_MIN) // 修复Android下编译重复定义警告
#define INT8_MIN	(int8_t)(-127 - 1)
#define INT16_MIN	(int16_t)(-32767 - 1)
#define INT32_MIN	(int32_t)(-2147483647 - 1)
#define INT64_MIN	(int64_t)(-9223372036854775807LL - 1)

#define INT8_MAX	(int8_t)(127)
#define INT16_MAX	(int16_t)(32767)
#define INT32_MAX	(int32_t)(2147483647)
#define INT64_MAX	(int64_t)(9223372036854775807LL)

#define UINT8_MAX	(uint8_t)(255)
#define UINT16_MAX	(uint16_t)(65535)
#define UINT32_MAX	(uint32_t)(4294967295U)
#define UINT64_MAX	(uint64_t)(18446744073709551615ULL)
#endif

#include <limits.h>

#endif


#if defined(_M_X64) || defined(__x86_64__)
#	ifndef SSIZE_MIN
#		define SSIZE_MIN       INT64_MIN
#	endif /* SSIZE_MAX */
#	ifndef SSIZE_MAX
#		define SSIZE_MAX       INT64_MAX
#	endif /* SSIZE_MAX */
#	ifndef SIZE_MAX
#		define SIZE_MAX        UINT64_MAX
#	endif /* SIZE_MAX */
#else
#	ifndef SSIZE_MIN
#		define SSIZE_MIN       INT32_MIN
#	endif /* SSIZE_MAX */
#	ifndef SSIZE_MAX
#		define SSIZE_MAX       INT32_MAX
#	endif /* SSIZE_MAX */
#	ifndef SIZE_MAX
#		define SIZE_MAX        UINT32_MAX
#	endif /* SIZE_MAX */
#endif


#if defined(_M_X64)
#	define SSIZE_FMT      "%lld"
#	define SIZE_FMT       "%llu"
#	define SSIZE_SCAN     "%lld"
#	define SIZE_SCAN      "%llu"
#elif defined(__x86_64__) || defined(__IOS__) || defined(_WIN32)// || defined(__ANDROID__)
#	define SSIZE_FMT      "%ld"
#	define SIZE_FMT       "%lu"
#	define SSIZE_SCAN     "%ld"
#	define SIZE_SCAN      "%lu"
#else
#	define SSIZE_FMT      "%d"
#	define SIZE_FMT       "%u"
#	define SSIZE_SCAN     "%d"
#	define SIZE_SCAN      "%u"
#endif


#ifdef GDK_CORE_DLL_BUILD
#	define GDK_CORE_EXPORT	__DECLSPEC__(__DLLEXPORT__)
#elif !defined(GDK_CORE_LIB_BUILD)
#	define GDK_CORE_EXPORT	__DECLSPEC__(__DLLIMPORT__)
#else
#	define GDK_CORE_EXPORT
#endif


#if defined(_MSC_VER)
#	pragma warning(disable:4251 4996 4127)
#endif


namespace gdk
{


#if defined(_UNICODE)
typedef  wchar_t	Char;
#else
typedef  char		Char;
#endif


struct Void {};


GDK_CORE_EXPORT int nothing(...);


#if !defined(_MSC_VER)

template<typename Ty>
__INLINE__ Ty max(const Ty &ty1, const Ty &ty2)
{
	return ty1 > ty2 ? ty1 : ty2;
}

template<typename Ty>
__INLINE__ Ty min(const Ty &ty1, const Ty &ty2)
{
	return ty1 < ty2 ? ty1 : ty2;
}

#endif



} //gdk





#endif //__GDK_TYPES_H__
